package com.example.coffeeseller_2.views.customviews;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

import com.example.coffeeseller_2.R;
import com.example.coffeeseller_2.model.beans.Coffee;

import java.util.List;

import cof.ac.inter.CoffMsger;
import cof.ac.inter.Result;

/**
 * Created by WH on 2018/6/28.
 * 商品测试
 */

public class ProductTestDialog {

    public void showProductTestDialog(List<Coffee> coffees, Context context, SendMkCoffeeCommandResult sendMkCoffeeCommandResult) {
        View view = LayoutInflater.from(context).inflate(R.layout.debug_dialog_product_layout, null, false);
        GridView gridView = (GridView) view.findViewById(R.id.productGrid);
        ProductGridAdapter adapter = new ProductGridAdapter(coffees, context, sendMkCoffeeCommandResult);
        gridView.setAdapter(adapter);
        final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setView(view);
        alertDialog.setButton(Dialog.BUTTON_NEGATIVE, "关闭", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (alertDialog.isShowing()) {
                    alertDialog.dismiss();
                }
            }
        });
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();
    }

    class ProductGridAdapter extends BaseAdapter {
        List<Coffee> coffees;
        Context context;
        CoffMsger coffMsger;
        SendMkCoffeeCommandResult sendMkCoffeeCommandResult;

        public ProductGridAdapter(List<Coffee> coffees, Context context, SendMkCoffeeCommandResult sendMkCoffeeCommandResult) {
            this.coffees = coffees;
            this.context = context;
            this.coffMsger = CoffMsger.getInstance();
            this.sendMkCoffeeCommandResult = sendMkCoffeeCommandResult;
        }

        @Override
        public int getCount() {
            return coffees.size();
        }

        @Override
        public Object getItem(int position) {
            return coffees.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.debug_grid_button, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
        }
            viewHolder.button.setText(coffees.get(position).name);
            viewHolder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Result result = coffMsger.mkCoffee(coffees.get(position).getProcessList());

                    if (sendMkCoffeeCommandResult != null) {
                        sendMkCoffeeCommandResult.getResult(result);
                    }

                }
            });
            return convertView;
        }

        class ViewHolder {
            public Button button;

            public ViewHolder(View view) {
                button = (Button) view.findViewById(R.id.product);
            }
        }
    }

    public interface SendMkCoffeeCommandResult {
        void getResult(Result result);
    }
}
